package views;

import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Text;

public class TextListenerFactory {

	public static KeyAdapter szovegesKeyListener() {
		return new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent key) {
				if ((!Character.isLetter(key.keyCode)
						&& !Character.isWhitespace(key.keyCode) && !Character
							.isISOControl(key.keyCode))) {
					key.doit = false;
				}

			}

		};
	}

	public static KeyAdapter textAndDigitsKeyListener() {
		return new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent key) {
				if ((!Character.isLetter(key.keyCode)
						&& !Character.isWhitespace(key.keyCode) && !Character
							.isISOControl(key.keyCode))
						&& !Character.isDigit(key.keyCode) && key.keyCode != 13) {
					key.doit = false;
				}

			}

		};
	}

	public static KeyAdapter userNameKeyListener() {
		return new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent key) {
				if ((!Character.isLetter(key.character))
						&& !Character.isISOControl(key.keyCode)
						&& !Character.isDigit(key.keyCode)
						&& !new String("!;.,-'%//=()[]_")
								.contains(key.character + "")) {
					key.doit = false;
				}

			}

		};
	}

	public static KeyAdapter passwordKeyListener() {
		return new KeyAdapter() {

			@Override
			public void keyPressed(KeyEvent key) {
				if ((!Character.isLetter(key.character))
						&& !Character.isISOControl(key.keyCode)
						&& !Character.isDigit(key.keyCode)
						&& !new String("!;.,-'%//=()[]_")
								.contains(key.character + "")) {
					key.doit = false;
				}

			}

		};
	}

	public static ModifyListener szovegHosszListener(final int hossz) {
		return new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent modify) {
				if (modify.getSource() instanceof Text) {
					Text origin = (Text) modify.getSource();
					if (origin.getText().length() > hossz) {
						origin.setText(origin.getText().substring(0, hossz));
					}
				} else {
					Combo origin = (Combo) modify.getSource();
					if (origin.getText().length() > hossz) {
						origin.setText(origin.getText().substring(0, hossz));
					}
				}

			}

		};
	}

}
